<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">


<meta name="google-site-verification" content="E9deYnivN5MuHMuIfiMZZfS0alv-d_0UjcwjBL79lGU" />



<meta name="baidu-site-verification" content="iHYWJxscwD" />










<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="true" />








  <meta name="baidu-site-verification" content="true" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Python,量化投资,机器学习,数据结构算法,基本数据结构," />










<meta name="description" content="还是补基础，学拉钩教育的《300分钟搞定数据结构与算法》https:&#x2F;&#x2F;kaiwu.lagou.com&#x2F;course&#x2F;courseInfo.htm?courseId&#x3D;3 课程的笔记。01.常用数据结构数组、字符串①示例:翻转字符串用两个指针分别指向字符串首尾，交换两个字符，指针分别向前后进一位。直到两个指针相遇。 123456789101112# 翻转字符串str &#x3D; &quot;algorith">
<meta property="og:type" content="article">
<meta property="og:title" content="量化投资学习笔记42——基本数据结构算法总结1">
<meta property="og:url" content="https://zwdnet.github.io/2020/04/21/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B042%E2%80%94%E2%80%94%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%931/index.html">
<meta property="og:site_name" content="赵瑜敏的口腔医学专业学习博客">
<meta property="og:description" content="还是补基础，学拉钩教育的《300分钟搞定数据结构与算法》https:&#x2F;&#x2F;kaiwu.lagou.com&#x2F;course&#x2F;courseInfo.htm?courseId&#x3D;3 课程的笔记。01.常用数据结构数组、字符串①示例:翻转字符串用两个指针分别指向字符串首尾，交换两个字符，指针分别向前后进一位。直到两个指针相遇。 123456789101112# 翻转字符串str &#x3D; &quot;algorith">
<meta property="og:locale">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/34/01.png">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">
<meta property="article:published_time" content="2020-04-21T04:19:22.000Z">
<meta property="article:modified_time" content="2020-08-30T05:52:04.000Z">
<meta property="article:author" content="赵瑜敏">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="量化投资">
<meta property="article:tag" content="机器学习">
<meta property="article:tag" content="数据结构算法">
<meta property="article:tag" content="基本数据结构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/34/01.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zwdnet.github.io/2020/04/21/量化投资学习笔记42——基本数据结构算法总结1/"/>





  <title>量化投资学习笔记42——基本数据结构算法总结1 | 赵瑜敏的口腔医学专业学习博客</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">赵瑜敏的口腔医学专业学习博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zwdnet.github.io/2020/04/21/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B042%E2%80%94%E2%80%94%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%931/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="赵瑜敏的口腔医学专业学习博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">量化投资学习笔记42——基本数据结构算法总结1</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-21T04:19:22+00:00">
                2020-04-21
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" itemprop="url" rel="index">
                    <span itemprop="name">量化投资</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  10
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>还是补基础，学拉钩教育的《300分钟搞定数据结构与算法》<a target="_blank" rel="noopener" href="https://kaiwu.lagou.com/course/courseInfo.htm?courseId=3">https://kaiwu.lagou.com/course/courseInfo.htm?courseId=3</a> 课程的笔记。<br>01.常用数据结构<br>数组、字符串<br>①示例:翻转字符串<br>用两个指针分别指向字符串首尾，交换两个字符，指针分别向前后进一位。直到两个指针相遇。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 翻转字符串</span></span><br><span class="line"><span class="built_in">str</span> = <span class="string">&quot;algorithm&quot;</span></span><br><span class="line">head = <span class="number">0</span></span><br><span class="line">tail = <span class="built_in">len</span>(<span class="built_in">str</span>) - <span class="number">1</span></span><br><span class="line">ls = [<span class="string">&quot;a&quot;</span>]*<span class="built_in">len</span>(<span class="built_in">str</span>)</span><br><span class="line"><span class="keyword">while</span> head &lt;= tail:</span><br><span class="line">    ls[head] = <span class="built_in">str</span>[tail]</span><br><span class="line">    ls[tail] = <span class="built_in">str</span>[head]</span><br><span class="line">    head += <span class="number">1</span></span><br><span class="line">    tail -= <span class="number">1</span></span><br><span class="line">result = <span class="string">&#x27;&#x27;</span>.join(ls)</span><br><span class="line">print(result)</span><br></pre></td></tr></table></figure>
<p>数组的优点:简单，索引查询效率O(1)。<br>数组的缺点:值查询，增删的效率O(n)。<br>②两个字符串s和t，判断t是否是s的异位词。<br>即字母数量相同，排列不同。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 2.两个字符串s和t，判断t是否是s的异位词。</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">isAnagram</span>(<span class="params">s, t</span>):</span></span><br><span class="line">    alpha = [<span class="built_in">chr</span>(i) <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">97</span>, <span class="number">123</span>)]</span><br><span class="line">    alphadic = &#123;char:<span class="number">0</span> <span class="keyword">for</span> char <span class="keyword">in</span> alpha&#125;</span><br><span class="line">    l1 = <span class="built_in">len</span>(s)</span><br><span class="line">    l2 = <span class="built_in">len</span>(t)</span><br><span class="line">    <span class="keyword">if</span> l1 != l2:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(l1):</span><br><span class="line">        alphadic[s[i]] += <span class="number">1</span></span><br><span class="line">        alphadic[t[i]] -= <span class="number">1</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">97</span>, <span class="number">123</span>):</span><br><span class="line">        <span class="keyword">if</span> alphadic[<span class="built_in">chr</span>(i)] != <span class="number">0</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">True</span></span><br></pre></td></tr></table></figure>
<p>力扣第242题，提交通过。<br>链表<br>优点，灵活使用内存空间，插入删除时间复杂度O(1)<br>缺点，只能顺序查找，复杂度O(n)。<br>③链表翻转问题<br>给定一个链表，每k个节点进行翻转。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">reverseKGroup</span>(<span class="params">head, k</span>):</span></span><br><span class="line">    h = ListNode(-<span class="number">1</span>)</span><br><span class="line">    h.<span class="built_in">next</span> = head</span><br><span class="line">    cur = pre = h</span><br><span class="line">   </span><br><span class="line">    n = -<span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> cur != <span class="literal">None</span>:</span><br><span class="line">        n += <span class="number">1</span></span><br><span class="line">        cur = cur.<span class="built_in">next</span></span><br><span class="line">       </span><br><span class="line">    <span class="keyword">while</span> n &gt;= k:</span><br><span class="line">        cur = pre.<span class="built_in">next</span></span><br><span class="line">        <span class="keyword">for</span> _ <span class="keyword">in</span> <span class="built_in">range</span>(k - <span class="number">1</span>):</span><br><span class="line">            lat = cur.<span class="built_in">next</span></span><br><span class="line">            cur.<span class="built_in">next</span> = lat.<span class="built_in">next</span></span><br><span class="line">            lat.<span class="built_in">next</span> = pre.<span class="built_in">next</span></span><br><span class="line">            pre.<span class="built_in">next</span> = lat</span><br><span class="line">        pre = cur</span><br><span class="line">        n -= k</span><br><span class="line">       </span><br><span class="line">    <span class="keyword">return</span> h.<span class="built_in">next</span></span><br></pre></td></tr></table></figure>
<p>力扣第25题，提交通过。第24题就是k = 2的情况，也提交通过了。<br>栈:后进先出，只能从栈顶进出。时间复杂度O(1)。<br>④给定一个只包含()[]{}的字符串，判断其是否有效。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 5.判断括号符号的有效性</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stack</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        self.data = []</span><br><span class="line">        self.length = <span class="number">0</span></span><br><span class="line">       </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">push</span>(<span class="params">self, val</span>):</span></span><br><span class="line">        self.data.append(val)</span><br><span class="line">        self.length += <span class="number">1</span></span><br><span class="line">       </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">pop</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">if</span> self.length == <span class="number">0</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line">        res = self.data.pop()</span><br><span class="line">        self.length -= <span class="number">1</span></span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line">       </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">getLength</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> self.length</span><br><span class="line">   </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get_item</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">if</span> self.length == <span class="number">0</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> self.data[self.length-<span class="number">1</span>]</span><br><span class="line">       </span><br><span class="line">   </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">isValid</span>(<span class="params">s</span>):</span></span><br><span class="line">    length = <span class="built_in">len</span>(s)</span><br><span class="line">    <span class="keyword">if</span> length == <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">    <span class="keyword">if</span> length % <span class="number">2</span> != <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">    stack = Stack()</span><br><span class="line">    left = [<span class="string">&quot;(&quot;</span>, <span class="string">&quot;[&quot;</span>, <span class="string">&quot;&#123;&quot;</span>]</span><br><span class="line">    right =  [<span class="string">&quot;)&quot;</span>, <span class="string">&quot;]&quot;</span>, <span class="string">&quot;&#125;&quot;</span>]</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(length):</span><br><span class="line">        c = s[i]</span><br><span class="line">        cp = <span class="literal">None</span></span><br><span class="line">        <span class="keyword">if</span> c <span class="keyword">in</span> left:</span><br><span class="line">            stack.push(c)</span><br><span class="line">        <span class="keyword">elif</span> c <span class="keyword">in</span> right:</span><br><span class="line">            cp = stack.pop()</span><br><span class="line">            <span class="keyword">if</span> ((c == <span class="string">&quot;)&quot;</span> <span class="keyword">and</span> cp != <span class="string">&quot;(&quot;</span>) <span class="keyword">or</span></span><br><span class="line">                 (c == <span class="string">&quot;]&quot;</span> <span class="keyword">and</span> cp != <span class="string">&quot;[&quot;</span>) <span class="keyword">or</span></span><br><span class="line">                 (c == <span class="string">&quot;&#125;&quot;</span> <span class="keyword">and</span> cp != <span class="string">&quot;&#123;&quot;</span>)):</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> stack.getLength() == <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">False</span></span><br></pre></td></tr></table></figure>
<p>力扣第20题，已提交通过。<br>⑤气温列表<br>给定一个每日气温列表，返回一个列表，数值为今天以后多长时间气温会回升。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 算法一，穷举，超时了</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dailyTemperatures</span>(<span class="params">T</span>):</span></span><br><span class="line">    n = <span class="built_in">len</span>(T)</span><br><span class="line">    result = [<span class="number">0</span>]*n</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line">        T0 = T[i]</span><br><span class="line">        d = <span class="number">1</span></span><br><span class="line">        bHigh = <span class="literal">False</span></span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(i+<span class="number">1</span>, n):</span><br><span class="line">            <span class="keyword">if</span> T[j] &gt; T[i]:</span><br><span class="line">                bHigh = <span class="literal">True</span></span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            d += <span class="number">1</span></span><br><span class="line">        <span class="keyword">if</span> bHigh == <span class="literal">False</span>:</span><br><span class="line">            d = <span class="number">0</span></span><br><span class="line">        result[i] = d</span><br><span class="line">    <span class="keyword">return</span> result</span><br></pre></td></tr></table></figure>
<p>能想到的最简单的方法是直接穷举，复杂度是O(n²)。力扣739题，提交显示超出时间限制。<br>再照教程用栈试试。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 算法2，用堆栈，降低时间复杂度</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dailyTemperatures2</span>(<span class="params">T</span>):</span></span><br><span class="line"><span class="comment"># print(T)</span></span><br><span class="line"><span class="comment"># n = len(T)</span></span><br><span class="line"><span class="comment"># result = [0]*n</span></span><br><span class="line"><span class="comment"># stack = Stack()</span></span><br><span class="line"><span class="comment"># for i in range(n):</span></span><br><span class="line"><span class="comment"># if stack.getLength() == 0:</span></span><br><span class="line"><span class="comment"># stack.push(i)</span></span><br><span class="line"><span class="comment"># T_top = T[i]</span></span><br><span class="line"><span class="comment"># continue</span></span><br><span class="line"><span class="comment"># top_index = stack.get_item()</span></span><br><span class="line"><span class="comment"># if T[i] &gt; T[top_index]:</span></span><br><span class="line"><span class="comment"># i_top = stack.pop()</span></span><br><span class="line"><span class="comment"># result[i_top] = i - i_top</span></span><br><span class="line"><span class="comment"># stack.push(i)</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># stack.push(i)</span></span><br><span class="line"><span class="comment"># print(i, T[i], top_index, T[top_index])</span></span><br><span class="line"><span class="comment"># return result</span></span><br><span class="line">    ans = [<span class="number">0</span>]*<span class="built_in">len</span>(T)</span><br><span class="line">    stack = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(T) - <span class="number">1</span>, -<span class="number">1</span>, -<span class="number">1</span>):</span><br><span class="line">        <span class="keyword">while</span> stack <span class="keyword">and</span> T[i] &gt;= T[stack[-<span class="number">1</span>]]:</span><br><span class="line">            stack.pop()</span><br><span class="line">        <span class="keyword">if</span> stack:</span><br><span class="line">            ans[i] = stack[-<span class="number">1</span>] - i</span><br><span class="line">        stack.append(i)</span><br><span class="line">    <span class="keyword">return</span> ans</span><br></pre></td></tr></table></figure>
<p>自己写的总是有问题，网上找了个现成的，通过了。<br>其它栈的题。<br>力扣224:输入含数字，括号和+-的字符串，计算结果。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 7.基本计算器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">calculate</span>(<span class="params">s</span>):</span></span><br><span class="line">    stack = []</span><br><span class="line">    res = <span class="number">0</span></span><br><span class="line">    num = <span class="number">0</span></span><br><span class="line">    sign = <span class="number">1</span></span><br><span class="line">    <span class="keyword">for</span> c <span class="keyword">in</span> s:</span><br><span class="line">        <span class="keyword">if</span> c.isdigit():</span><br><span class="line">            num = num*<span class="number">10</span> + <span class="built_in">int</span>(c)</span><br><span class="line">        <span class="keyword">elif</span> c == <span class="string">&quot;+&quot;</span> <span class="keyword">or</span> c == <span class="string">&quot;-&quot;</span>:</span><br><span class="line">            res = res + num*sign</span><br><span class="line">            <span class="keyword">if</span> c == <span class="string">&quot;+&quot;</span>:</span><br><span class="line">                sign = <span class="number">1</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                sign = -<span class="number">1</span></span><br><span class="line">            num = <span class="number">0</span></span><br><span class="line">        <span class="keyword">elif</span> c == <span class="string">&quot;(&quot;</span>:</span><br><span class="line">            stack.append(res)</span><br><span class="line">            stack.append(sign)</span><br><span class="line">            sign = <span class="number">1</span></span><br><span class="line">            res = <span class="number">0</span></span><br><span class="line">        <span class="keyword">elif</span> c == <span class="string">&quot;)&quot;</span>:</span><br><span class="line">            res = res + sign*num</span><br><span class="line">            old_sign = stack.pop()</span><br><span class="line">            old_res = stack.pop()</span><br><span class="line">            res = old_res + old_sign*res</span><br><span class="line">            sign = <span class="number">1</span></span><br><span class="line">            num = <span class="number">0</span></span><br><span class="line">    res = res + sign*num</span><br><span class="line">    <span class="keyword">return</span> res</span><br></pre></td></tr></table></figure>
<p>力扣227，增加乘除法的计算器。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 高级计算器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">highCulate</span>(<span class="params">s</span>):</span></span><br><span class="line">    <span class="comment"># 初始化sign为 “+”，是因为开头是数字</span></span><br><span class="line">    num ,stack ,sign = <span class="number">0</span> , [] , <span class="string">&#x27;+&#x27;</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(s)):</span><br><span class="line">        ch = s[i]</span><br><span class="line">        <span class="keyword">if</span> ch.isdigit():</span><br><span class="line">            num = num * <span class="number">10</span> + <span class="built_in">int</span>(ch) <span class="comment">#根据当前数字之前的符号，来决定如何处理当前数值</span></span><br><span class="line">        <span class="comment"># 并将处理后的数值压入栈中</span></span><br><span class="line">        <span class="keyword">if</span> ch <span class="keyword">in</span> <span class="string">&quot;+-*/&quot;</span> <span class="keyword">or</span> i == <span class="built_in">len</span>(s)-<span class="number">1</span>:</span><br><span class="line">            <span class="keyword">if</span> sign == <span class="string">&quot;+&quot;</span> :</span><br><span class="line">                stack.append(num)</span><br><span class="line">            <span class="keyword">elif</span> sign == <span class="string">&quot;-&quot;</span> :</span><br><span class="line">                stack.append(-num)</span><br><span class="line">            <span class="keyword">elif</span> sign == <span class="string">&quot;*&quot;</span>:</span><br><span class="line">                stack.append(stack.pop() * num)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                stack.append(<span class="built_in">int</span>(stack.pop()/num))</span><br><span class="line">            num = <span class="number">0</span></span><br><span class="line">            sign = ch</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">sum</span>(stack)</span><br></pre></td></tr></table></figure>
<p>力扣84题，柱状图最大矩形面积</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 9.柱状图最大矩形</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">maxJu</span>(<span class="params">heights</span>):</span></span><br><span class="line">    heights.append(<span class="number">0</span>)</span><br><span class="line">    stack = []</span><br><span class="line">    maxArea = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(heights)):</span><br><span class="line">        <span class="keyword">while</span> stack <span class="keyword">and</span> heights[i] &lt; heights[stack[-<span class="number">1</span>]]:</span><br><span class="line">            top = stack.pop()</span><br><span class="line">            wide = <span class="number">0</span></span><br><span class="line">            <span class="keyword">if</span> stack:</span><br><span class="line">                wide = i - stack[-<span class="number">1</span>] -<span class="number">1</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                wide = i</span><br><span class="line">            maxArea = <span class="built_in">max</span>(maxArea, heights[top] * wide)</span><br><span class="line">        stack.append(i)</span><br><span class="line">    <span class="keyword">return</span> maxArea</span><br><span class="line">```    </span><br><span class="line">队列:先进先出。</span><br><span class="line">应用:需要顺序处理，数据量在变化。</span><br><span class="line">双端队列:在队列头尾都可以O(<span class="number">1</span>)复杂度查看和增删。</span><br><span class="line">力扣<span class="number">239</span>题，给定一个数组和窗口大小k，返回每个窗口中的最大值。</span><br><span class="line">先用最直接的算法</span><br><span class="line">```python</span><br><span class="line"><span class="comment"># 滑动窗口最大值</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">maxSlidingWindow</span>(<span class="params">nums, k</span>):</span></span><br><span class="line">    i = <span class="number">0</span></span><br><span class="line">    n = <span class="built_in">len</span>(nums)</span><br><span class="line">    res = [<span class="number">0</span>]*(n-k+<span class="number">1</span>)</span><br><span class="line">    <span class="keyword">while</span> i &lt;= n - k:</span><br><span class="line">        temp = nums[i:i+k]</span><br><span class="line">        res[i] = <span class="built_in">max</span>(temp)</span><br><span class="line">        i += <span class="number">1</span></span><br><span class="line">    <span class="keyword">return</span> res</span><br></pre></td></tr></table></figure>
<p>再用双端队列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 用双端队列求解</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">maxSlidingWindow2</span>(<span class="params">nums, k</span>):</span></span><br><span class="line">    deque = collections.deque()</span><br><span class="line">    res = []</span><br><span class="line">    <span class="keyword">for</span> i, num <span class="keyword">in</span> <span class="built_in">enumerate</span>(nums):</span><br><span class="line">        <span class="keyword">while</span> deque <span class="keyword">and</span> deque[<span class="number">0</span>] &lt;= i - k: </span><br><span class="line">            deque.popleft()</span><br><span class="line">        <span class="keyword">while</span> deque <span class="keyword">and</span> num &gt; nums[deque[-<span class="number">1</span>]]:</span><br><span class="line">            deque.pop()</span><br><span class="line">        deque.append(i)</span><br><span class="line">        <span class="keyword">if</span> i &gt;= k-<span class="number">1</span>:</span><br><span class="line">            res.append(nums[deque[<span class="number">0</span>]])</span><br><span class="line">    <span class="keyword">return</span> res</span><br></pre></td></tr></table></figure>
<p>提交，运行时间比第一个快了很多。<br>树的特点是有递归形式。<br>树的遍历有前序，中序和后序三种，以访问根结点的顺序区分。<br>参考<br><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/55577744">https://zhuanlan.zhihu.com/p/55577744</a><br>照着撸一遍吧。<br><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/blog0178-QTLearn/34/01.png"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding:utf-8</span></span><br><span class="line"><span class="comment"># 树的各种遍历方式</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 节点类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, item</span>):</span></span><br><span class="line">        self.item = item</span><br><span class="line">        self.left = <span class="literal">None</span></span><br><span class="line">        self.right = <span class="literal">None</span></span><br><span class="line">       </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">str</span>(self.item)</span><br><span class="line">       </span><br><span class="line">       </span><br><span class="line"><span class="comment"># 二叉树类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tree</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        self.root = Node(<span class="string">&quot;root&quot;</span>)</span><br><span class="line">       </span><br><span class="line">    <span class="comment"># 增加节点</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">add</span>(<span class="params">self, item</span>):</span></span><br><span class="line">        node = Node(item)</span><br><span class="line">        <span class="keyword">if</span> self.root <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            self.root = node</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            q = [self.root]</span><br><span class="line">           </span><br><span class="line">            <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">                pop_node = q.pop(<span class="number">0</span>)</span><br><span class="line">                <span class="keyword">if</span> pop_node.left <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">                    pop_node.left = node</span><br><span class="line">                    <span class="keyword">return</span></span><br><span class="line">                <span class="keyword">elif</span> pop_node.right <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">                    pop_node.right = node</span><br><span class="line">                    <span class="keyword">return</span></span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    q.append(pop_node.left)</span><br><span class="line">                    q.append(pop_node.right)</span><br><span class="line">                   </span><br><span class="line">    <span class="comment"># 先序遍历</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">preorder</span>(<span class="params">self, root</span>):</span></span><br><span class="line">        <span class="keyword">if</span> root <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">return</span> []</span><br><span class="line">        result = [root.item]</span><br><span class="line">        left_item = self.preorder(root.left)</span><br><span class="line">        right_item = self.preorder(root.right)</span><br><span class="line">        <span class="keyword">return</span> result + left_item + right_item</span><br><span class="line">       </span><br><span class="line">    <span class="comment"># 中序遍历</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">inorder</span>(<span class="params">self, root</span>):</span></span><br><span class="line">        <span class="keyword">if</span> root <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">return</span> []</span><br><span class="line">        result = [root.item]</span><br><span class="line">        left_item = self.inorder(root.left)</span><br><span class="line">        right_item = self.inorder(root.right)</span><br><span class="line">        <span class="keyword">return</span> left_item + result + right_item</span><br><span class="line">       </span><br><span class="line">    <span class="comment"># 后序遍历</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">postorder</span>(<span class="params">self, root</span>):</span></span><br><span class="line">        <span class="keyword">if</span> root <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">return</span> []</span><br><span class="line">        result = [root.item]</span><br><span class="line">        left_item = self.postorder(root.left)</span><br><span class="line">        right_item = self.postorder(root.right)</span><br><span class="line">        <span class="keyword">return</span> left_item + right_item + result</span><br><span class="line">       </span><br><span class="line">    <span class="comment"># 层次遍历</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">traverse</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">if</span> self.root <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line">        q = [self.root]</span><br><span class="line">        res = [self.root.item]</span><br><span class="line">        <span class="keyword">while</span> q != []:</span><br><span class="line">            pop_node = q.pop(<span class="number">0</span>)</span><br><span class="line">            <span class="keyword">if</span> pop_node.left <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                q.append(pop_node.left)</span><br><span class="line">                res.append(pop_node.left.item)</span><br><span class="line">               </span><br><span class="line">            <span class="keyword">if</span> pop_node.right <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                q.append(pop_node.right)</span><br><span class="line">                res.append(pop_node.right.item)</span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line">           </span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    t = Tree()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">        t.add(i)</span><br><span class="line">    print(<span class="string">&quot;前序遍历:&quot;</span>, t.preorder(t.root))</span><br><span class="line">    print(<span class="string">&quot;中序遍历:&quot;</span>, t.inorder(t.root))</span><br><span class="line">    print(<span class="string">&quot;后序遍历:&quot;</span>, t.postorder(t.root))</span><br><span class="line">    print(<span class="string">&quot;层次遍历:&quot;</span>, t.traverse())</span><br></pre></td></tr></table></figure>
<p>结果<br>前序遍历: [‘root’, 0, 2, 6, 7, 3, 8, 9, 1, 4, 5]<br>中序遍历: [6, 2, 7, 0, 8, 3, 9, ‘root’, 4, 1, 5]<br>后序遍历: [6, 7, 2, 8, 9, 3, 0, 4, 5, 1, ‘root’]<br>层次遍历: [‘root’, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] </p>
<p>树的应用<br>力扣230题，给定二叉搜索树，找到第k个最小的元素。<br>对二叉搜索树进行中序遍历，即为对数据进行排序，其第k个值即为所求。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 力扣230题，第k大的元素</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TreeNode</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, x</span>):</span></span><br><span class="line">        self.val = x</span><br><span class="line">        self.left = <span class="literal">None</span></span><br><span class="line">        self.right = <span class="literal">None</span></span><br><span class="line">   </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">kthSmallest</span>(<span class="params">root, k</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">inorder</span>(<span class="params">root</span>):</span></span><br><span class="line">        <span class="keyword">return</span> inorder(root.left) + [root.val] + inorder(root.right) <span class="keyword">if</span> r <span class="keyword">else</span> []</span><br><span class="line">    <span class="keyword">return</span> inorder(root)[k-<span class="number">1</span>]</span><br></pre></td></tr></table></figure>
<p>提交通过。</p>
<p>本篇的代码： <a target="_blank" rel="noopener" href="https://github.com/zwdnet/MyQuant/tree/master/42">https://github.com/zwdnet/MyQuant/tree/master/42</a><br>以前学过的，但是一动手才发现很多时候都写不出来，要参考别人写的。实际干活还是用最简单的线性结构，算法第一个念头就是穷举。不知道我是该去刷题呢还是做中学，碰到了再说？先把这个系列学完吧。</p>
<p>我发文章的三个地方，欢迎大家在朋友圈等地方分享，欢迎点“在看”。<br>我的个人博客地址：<a href="https://zwdnet.github.io/">https://zwdnet.github.io</a><br>我的知乎文章地址： <a target="_blank" rel="noopener" href="https://www.zhihu.com/people/zhao-you-min/posts">https://www.zhihu.com/people/zhao-you-min/posts</a><br>我的微信个人订阅号：赵瑜敏的口腔医学学习园地</p>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg"></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>欢迎打赏！感谢支持！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/mm_facetoface_collect_qrcode_1542944836634.png" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/1542944857770.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
            <a href="/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" rel="tag"># 量化投资</a>
          
            <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" rel="tag"># 机器学习</a>
          
            <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AE%97%E6%B3%95/" rel="tag"># 数据结构算法</a>
          
            <a href="/tags/%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="tag"># 基本数据结构</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/18/%E4%BD%A0%E4%BC%9A%E5%A4%87%E7%89%99%E5%90%97%E2%80%94%E2%80%94%E4%BF%9D%E6%8A%A4%E7%89%99%E5%91%A8%EF%BC%8C%E6%8E%A5%E8%AF%8A%E6%B5%81%E7%A8%8B%EF%BC%8C%E5%87%86%E5%A4%87/" rel="next" title="你会备牙吗——保护牙周，接诊流程，准备">
                <i class="fa fa-chevron-left"></i> 你会备牙吗——保护牙周，接诊流程，准备
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/04/21/%E7%89%99%E9%AB%93%E7%97%85%E6%80%BB%E7%BB%9315%E2%80%94%E2%80%94%E7%83%AD%E7%89%99%E8%83%B6%E6%A0%B9%E5%85%85%E7%9A%84%E4%B8%80%E4%BA%9B%E9%97%AE%E9%A2%98/" rel="prev" title="牙髓病总结15——热牙胶根充的一些问题">
                牙髓病总结15——热牙胶根充的一些问题 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MTA2Mi8xNzU4Nw=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">452</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">544</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">本站版权归赵瑜敏所有，如欲转载请与本人联系。</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">1225.8k</span>
  
</div>









<div>
  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1275447216'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1275447216%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

        







  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1275447216&web_id=1275447216" language="JavaScript"></script>
  </div>



        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  
</body>
</html>
